package me.sdevil507.biz.fruit.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.*;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.po.FruitPO;
import me.sdevil507.biz.fruit.po.FruitWxAppletPO;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: tips_back_end
 **/
@RestController
@RequestMapping("/api/common/fruit")
@Slf4j
public class FruitCommonController {

    @Autowired
    private FruitService fruitService;

    @Autowired
    private FruitMapper fruitMapper;

    /**
     * 获取单个成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/getFruitById")
    public ApiResultDTO getFruitById(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            FruitDTO fruitDTO = fruitMapper.po2dto(fruitService.findOneById(dto.getId()));

            if(fruitDTO.getInitFruitId() != null){
                FruitDTO fruitSearchDTO = new FruitDTO();
                fruitSearchDTO.setInitFruitId(fruitDTO.getInitFruitId());
                List<ResultTagFruitDTO> tagList = fruitService.readAllTagFruitList(fruitSearchDTO);
                fruitDTO.setAllTagFruitList(tagList);
            }

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), fruitDTO);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }



    /**
     * 获取单个成果
     *
     * @param po 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/createFruitWxAppletSimple")
    public ApiResultDTO createFruitWxAppletSimple(@RequestBody FruitWxAppletPO po) {
        ApiResultDTO apiResultDTO;
        try {
            String content = po.getContent();
            if(!StringUtils.isEmpty(content)) {
                String[] contArr = content.split("\n");
                for (String cont : contArr) {
                    FruitWxAppletPO newPo = new FruitWxAppletPO();
                    newPo.setMobile(po.getMobile());
                    newPo.setAddress(po.getAddress());
                    newPo.setIsDeal(false);
                    newPo.setContent(cont);
                    fruitService.createFruitWxAppletSimple(newPo);
                }
            }
            String picUrl = po.getPicUrl();
            if(!StringUtils.isEmpty(picUrl)) {
                String[] picUrlArr = picUrl.split(",");
                for (String pur : picUrlArr) {
                    FruitWxAppletPO newPo = new FruitWxAppletPO();
                    newPo.setMobile(po.getMobile());
                    newPo.setAddress(po.getAddress());
                    newPo.setIsDeal(false);
                    newPo.setPicUrl(pur);
                    fruitService.createFruitWxAppletSimple(newPo);
                }
            }
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }
}
